import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import CopyrightRegistration from '@/views/CopyrightRegistration.vue'
import CopyrightMarket from '@/views/CopyrightMarket.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/views/About.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录/注册'
    }
  },
  {
    path: '/auth',
    component: () => import('@/views/auth/Layout.vue'),
    children: [
      {
        path: 'register',
        name: 'UserRegister',
        component: () => import('@/views/auth/Register.vue')
      },
      {
        path: 'forgot-password',
        name: 'ForgotPassword',
        component: () => import('@/views/auth/ForgotPassword.vue')
      }
    ]
  },
  {
    path: '/legal',
    component: () => import('@/views/legal/Layout.vue'),
    children: [
      {
        path: 'agreement',
        name: 'Agreement',
        component: () => import('@/views/legal/Agreement.vue')
      },
      {
        path: 'privacy',
        name: 'Privacy',
        component: () => import('@/views/legal/Privacy.vue')
      }
    ]
  },
  {
    path: '/copyright-registration',
    name: 'CopyrightRegistration',
    component: () => import('@/views/CopyrightRegistration.vue')
  },
  {
    path: '/copyright/copyright-evidence',
    name: 'CopyrightEvidence',
    component: () => import('@/views/copyright/CopyrightEvidence.vue')
  },
  {
    path: '/copyright-market',
    name: 'CopyrightMarket',
    component: () => import('@/views/CopyrightMarket.vue')
  },
  {
    path: '/zhima-ai',
    name: 'ZhimaAI',
    component: () => import('@/views/ZhimaAI.vue')
  },
  {
    path: '/solutions',
    name: 'AISolutions',
    component: () => import('@/views/AISolutions.vue')
  },
  {
    path: '/about-us',
    name: 'About',
    component: () => import('@/views/About.vue')
  },
  {
    path: '/app-copyright-registration',
    name: 'AppCopyrightRegistration',
    component: () => import('@/views/AppCopyrightRegistration.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 需要登录的路由
  const requiresAuth = [
    '/copyright/evidence',
    '/copyright/register',
    '/copyright/monitor',
    '/copyright/evidence-collection',
    '/copyright/rights-protection',
    '/copyright/evidence-verification'
  ];

  // 检查是否需要登录
  if (requiresAuth.includes(to.path)) {
    // 这里可以根据实际情况检查用户是否已登录
    const isAuthenticated = localStorage.getItem('token');
    if (!isAuthenticated) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router
